import { ERoutePath } from ':/routes/const';
import { userSelector } from ':/store/selice/user';
import React, { ReactNode, useEffect } from 'react';
import { useSelector } from 'react-redux';
import { useLocation, useNavigate } from 'react-router';

export interface IProps {
  children?: ReactNode;
}

const Auth: React.FC<IProps> = ({ children }) => {
  const user = useSelector(userSelector.selectData);
  const navigate = useNavigate();
  const location = useLocation();
  useEffect(() => {
    if (location.pathname !== ERoutePath.LOGIN && !user) {
      navigate(ERoutePath.LOGIN, { state: { redirect: location.pathname } });
    }
  }, [location.pathname, user]);

  return children;
}

export default Auth;